package com.aifeng.fragment

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.aifeng.base.BaseFragment
import com.aifeng.base.refresh
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.base.startActivityEx
import com.aifeng.common_ui.helper.formatDecimal
import com.aifeng.common_ui.helper.getString
import com.aifeng.common_ui.helper.optNotNull
import com.aifeng.common_ui.helper.toNotDouble
import com.aifeng.hassan_food.sale.HistoryActivity
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.OrderViewModel
import com.jeremyliao.liveeventbus.utils.observe
import kotlinx.android.synthetic.main.fragment_main_four.*
import org.json.JSONObject
import org.koin.androidx.viewmodel.ext.android.getViewModel

class MainFourFragment : BaseFragment() {

    //调用这个方法切换时不会释放掉Fragment
    override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
        this.view?.visibility = if (menuVisible) View.VISIBLE else View.GONE
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.fragment_main_four, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initTitle()

        four_swipe.isRefreshing = true
        getData()
    }

    override fun getViewModel() = getViewModel<OrderViewModel>()

    override fun initTitle() {
        four_swipe.refresh { getData() }

        four_history.oneClick { startActivityEx<HistoryActivity>() }

        observe("saleList") {
            four_swipe.isRefreshing = true
            getData()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().sale(getString("userId"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByNoError(
                event = getViewModel(),
                onSuccess = {
                    val person = JSONObject(it).optJSONObject("salePerson") ?: JSONObject()
                    four_day.text = person.optNotNull("days").formatDecimal()
                    four_month.text = person.optNotNull("months").formatDecimal()
                    four_lei.text = person.optNotNull("total").formatDecimal()

                    val team = JSONObject(it).optJSONObject("saleTeam") ?: JSONObject()
                    four_day1.text = team.optNotNull("days").formatDecimal()
                    four_month2.text = team.optNotNull("months").formatDecimal()

                    val personMonth = JSONObject(it).optJSONObject("bonusPerson") ?: JSONObject()
                    four_reward.text = personMonth.optNotNull("amount").formatDecimal()
                    four_time.text = "上次奖金计算时间：${personMonth.optNotNull("date")}"

                    val teamMonth = JSONObject(it).optJSONObject("bonusTeam") ?: JSONObject()
                    four_reward1.text = teamMonth.optNotNull("amount").formatDecimal()
                    four_percent.text =
                        "${teamMonth.optNotNull("percent")}\n${teamMonth.optNotNull("level")}"
                    four_percent1.text =
                        "${teamMonth.optNotNull("nextPercent")}\n${teamMonth.optNotNull("nextLevel")}"

                    val teamTotal = team.optNotNull("months").toNotDouble()
                    val levelTotal = teamMonth.optNotNull("level").toNotDouble()
                    val nextTotal = teamMonth.optNotNull("nextLevel").toNotDouble()

                    if (nextTotal - teamTotal > 0) {
                        four_many.text = "还差${(nextTotal - teamTotal).formatDecimal()}元晋升到更高返点"
                    } else {
                        four_many.text = "还差0元晋升到更高返点"
                    }

                    val percent = (teamTotal - levelTotal) / (nextTotal - levelTotal) * 100
                    four_progress.apply {
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                            setProgress(percent.toInt(), true)
                        } else progress = percent.toInt()
                    }
                },
                onFinally = { four_swipe.isRefreshing = false }
            )
    }

}
